package org.hhq.demo.config.filter;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Mono;
import java.time.LocalDate;

@Slf4j
@Component
public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<AuthGatewayFilterFactory.AuthConfig> {

    public AuthGatewayFilterFactory(){
        super(AuthConfig.class);
    }
    @Override
    public GatewayFilter apply(AuthConfig config) {
        return ((exchange, chain) -> {
            log.info("鉴权拦截；date:{};config:{}", LocalDate.now(),config);
            if (StringUtils.isEmpty(config.token)){
                return Mono.error(new Exception("请传入token"));
            }
            return chain.filter(exchange);
        });
    }

    @Data
    static class AuthConfig{
        private String name;
        private String token;
    }

}
